package com.che.software.testato.util.jsf.faces;

import java.io.Serializable;
import java.util.Locale;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Controller used to manage locales and languages.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since June, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Component(value = "localeController")
@ManagedBean(name = "localeController")
@Scope("session")
@SessionScoped
public class LocaleController implements Serializable {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(LocaleController.class);
	private static final long serialVersionUID = -9205001014230425880L;
	private static final String ATTRIBUTE_LOCALE = "locale";

	/**
	 * Members.
	 */
	private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

	/**
	 * Get the language related to the current locale.
	 * 
	 * @return the corresponding string.
	 */
	public String getLanguage() {
		return locale.getLanguage();
	}

	/**
	 * Listener for the locale change events.
	 * 
	 * @param e the event which just happened.
	 */
	public void navigationCompute(ActionEvent e) {
		LOGGER.debug("navigationCompute(" + FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(ATTRIBUTE_LOCALE) + ").");
		setLanguage(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(ATTRIBUTE_LOCALE)));
	}

	/**
	 * Redirection to perform after a locale change event.
	 * 
	 * @return the target url.
	 */
	public String onNavigationCompute() {
		LOGGER.debug("onNavigationCompute().");
		return "../index.xhtml?faces-redirect=true";
	}

	/**
	 * Set a new language and consequently, a new locale.
	 * 
	 * @param language the new language to set.
	 */
	public void setLanguage(String language) {
		LOGGER.debug("setLanguage(" + language + ").");
		locale = new Locale(language);
		FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
	}

	/**
	 * Getter for the private field value ATTRIBUTE_LOCALE.
	 * 
	 * @return the ATTRIBUTE_LOCALE field value.
	 */
	public String getAttributeLocale() {
		return ATTRIBUTE_LOCALE;
	}

	/**
	 * Getter for the private field value locale.
	 * 
	 * @return the locale field value.
	 */
	public Locale getLocale() {
		return locale;
	}
}